import request from '@/utils/request'
import {
  Product,
  ProductType,
  ProductListParams,
  ProductCategory,
  AttrRuleValue,
  ProductAttrTpl,
  IExpressTemplate,
  AttrTableHeader,
  ProductAttr,
  ProductCategoryParams,
  ProductRuleParams,
  ProductRule,
  ProductReply
} from './types/product'
import { IFormData } from './types/form'

/**
 * 获取商品类目列表
 * @returns 商品类目列表
 */
export const getProductTypes = () => {
  return request<{
    list: ProductType[]
  }>({
    method: 'GET',
    url: '/product/product/type_header'
  })
}

/**
 * 获取商品列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getProducts = (params?: ProductListParams) => {
  return request<{
    count: number
    list: Product[]
  }>({
    method: 'GET',
    url: '/product/product',
    params
  })
}

/**
 * 保存新增或编辑
 * @param id 商品ID
 * @param data 商品数据
 * @returns null
 */
export const saveProduct = (id: number, data: any) => {
  return request({
    method: 'POST',
    url: `/product/product/${id}`,
    data
  })
}

/**
 * 获取商品
 * @param id 商品id
 * @returns 商品
 */
export const getProduct = (id: number) => {
  return request<{
    tempList: {
      id: number
      name: string
    }[]
    cateList: {
      value: number
      label: string
      disabled: number
    }[]
    productInfo: {
      cate_id: string[]
      is_sub: number[]
      activity: string[]
      label_id: string[]
      coupons: string[]
      description: string
      items: string[]
      attrs: string[]
      attr: {
        pic: string
        vip_price: number
        price: number
        cost: number
        ot_price: number
        stock: number
        bar_code: string
        weight: number
        volume: number
        brokerage: number
        brokerage_two: number
      }
    } & Omit<Product, 'cate_id' | 'is_sub' | 'activity' | 'label_id' | 'collect' | 'likes' | 'visitor' | 'cate_name' | 'stock_attr'>
  }>({
    method: 'GET',
    url: `/product/product/${id}`
  })
}

export const getCategoryTree = (type: 0 | 1) => {
  return request<ProductCategory[]>({
    method: 'GET',
    url: `/product/category/tree/${type}`
  })
}

/**
 * 商品上下架操作
 */
export const updateProductStatus = (id: number, status: number) => {
  return request({
    method: 'PUT',
    url: `/product/product/set_show/${id}/${status}`
  })
}

/**
 * 商品加入/移除回收站
 */
export const removeProduct = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/product/${id}`
  })
}

/**
 * 获取商品规格模板
 */
export const getAttrs = () => {
  return request<ProductAttrTpl[]>({
    method: 'GET',
    url: '/product/product/get_rule'
  })
}

/**
 * 生成商品属性
 */
export const generateAttr = (id: number, type: 0 | 1, data: {
  attrs: AttrRuleValue[]
}) => {
  return request<{
    info: {
      attr: AttrRuleValue[]
      header: AttrTableHeader[]
      value: ProductAttr[]
    }
  }>({
    method: 'POST',
    url: `/product/generate_attr/${id}/${type}`,
    data
  })
}

/**
 * 获取运费模板
 */
export const getExpressTemplate = () => {
  return request<IExpressTemplate[]>({
    method: 'GET',
    url: '/product/product/get_template'
  })
}

/**
 * 获取商品规格列表
 */
export const getProductRules = () => {
  return request({
    method: 'GET',
    url: '/product/product/rule'
  })
}

/**
 * 批量上架
 */
export const updateProductsShow = (ids: number[]) => {
  return request({
    method: 'PUT',
    url: '/product/product/product_show',
    data: {
      ids
    }
  })
}

/**
 * 批量下架
 */
export const updateProductsUnshow = (ids: number[]) => {
  return request({
    method: 'PUT',
    url: '/product/product/product_unshow',
    data: {
      ids
    }
  })
}

/**
 * 获取商品分类列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getProductCategory = (params?: ProductCategoryParams) => {
  return request<{
    count: number
    list: ProductCategory[]
  }>({
    method: 'GET',
    url: '/product/category',
    params
  })
}

/**
 * 商品分类状态操作
 */
export const updateCategoryStatus = (id: number, status: number) => {
  return request({
    method: 'PUT',
    url: `/product/category/set_show/${id}/${status}`
  })
}

/**
 * 商品分类删除
 */
export const removeCategory = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/category/${id}`
  })
}

/**
 * 新增分类
 * @param data 商品分类数据
 * @returns null
 */
export const saveCategory = (data: ProductCategory) => {
  return request({
    method: 'POST',
    url: '/product/category',
    data
  })
}

/**
 * 编辑分类
 * @param id 商品分类id
 * @param data 商品分类数据
 * @returns null
 */
export const updateCategory = (data: ProductCategory, id: number) => {
  return request({
    method: 'PUT',
    url: `/product/category/${id}`,
    data
  })
}

/**
 * 快捷编辑分类
 * @param id 商品分类id
 * @returns null
 */
export const updateSetCategory = (id: number) => {
  return request({
    method: 'PUT',
    url: `/product/category/set_category/${id}`
  })
}

/**
 * 商品分类新增表单
 */
export const getCategoryCreateForm = () => {
  return request<IFormData[]>({
    method: 'GET',
    url: '/product/category/create'
  })
}

/**
 * 商品分类编辑表单
 */
export const getCategoryEditForm = (id: number) => {
  return request<IFormData[]>({
    method: 'GET',
    url: `/product/category/${id}/edit`
  })
}

/**
 * 获取商品规格列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getProductRule = (params?: ProductRuleParams) => {
  return request<{
    count: number
    list: ProductRule[]
  }>({
    method: 'GET',
    url: '/product/product/rule',
    params
  })
}

/**
 * 保存新增或编辑规格
 * @param id 规格ID
 * @param data 规格数据
 * @returns null
 */
export const saveProductRule = (data: any, id?: number) => {
  return request({
    method: 'POST',
    url: `/product/product/rule/${id}`,
    data
  })
}

/**
 * 商品规格删除
 */
export const removeProductRule = (data: any) => {
  return request({
    method: 'DELETE',
    url: '/product/product/rule/delete',
    data
  })
}

/**
 * 商品规格详情
 */
export const getProductDetail = (id: number) => {
  return request<ProductRule>({
    method: 'GET',
    url: `/product/product/rule/${id}`
  })
}

/**
 * 商品评论删除
 */
export const removeProductReply = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/product/reply/${id}`
  })
}

/**
 * 获取商品评论列表
 * @param params 查询参数
 * @returns 商品列表
 */
export const getProductReply = (params?: ProductRuleParams) => {
  return request<{
    count: number
    list: ProductReply[]
  }>({
    method: 'GET',
    url: '/product/reply',
    params
  })
}

/**
 * 商品评论回复
 */
export const setProductReply = (id: number, data: any) => {
  return request({
    method: 'PUT',
    url: `/product/reply/set_reply/${id}`,
    data
  })
}

/**
 * 添加虚拟评论
 */
export const saveFictitiousReply = (data: any) => {
  return request({
    method: 'POST',
    url: '/product/reply/save_fictitious_reply',
    data
  })
}

/**
 * 虚拟评论表单
 */
export const fictitiousReplyList = (id: number) => {
  return request<{
    count: number
    list: Product[]
  }>({
    method: 'GET',
    url: `/product/reply/fictitious_reply/${id}`
  })
}
